/**
 * klasse zur verwaltung der wichtigen einstellungen und variablen.
 * 
 */
package ch.unizh.ifi.ailab.config;

/**
 * @author hans jaeckle jaeckle@gmail.com
 * 
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import ch.unizh.ifi.ailab.utils.Utils;

public class ProgramSettings extends RobosuitcaseProperties {

	private static final long serialVersionUID = 1L;

	private static final String ENCODING = "UTF-8"; 

	private String currentPropertiesPath = Utils.path(System
			.getProperty("user.dir"), ("conf"
			+ System.getProperty("file.separator") + "current.prop"));

	private static ProgramSettings myProperties = null;

	// logging
	// private static Logger myLog = Logger.getLogger(ProgramSettings.class);

	// singleton zum sicherstellen, dass nur eine instanz im programm!
	private ProgramSettings() {
		configDefaults();
		// load saved properties
		FileOutputStream os = null;
		try {
			File currentPath = new File(currentPropertiesPath);
			if (!currentPath.exists()) {
				os = new FileOutputStream(currentPath);
				storeToXML(os, "default properties");
				os.close();
			} 
			loadFromXML(new FileInputStream(currentPath));
			System.out.println(" current settings loaded");
			setProperty("currentFile", "current.prop");
		} catch (InvalidPropertiesFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static ProgramSettings getProgramSettings() {
		if (myProperties == null) {
			myProperties = new ProgramSettings();
		}
		return myProperties;
	}

	public void configDefaults() {
		// initialize with defaultvalues
		// myLog.debug("Inizialisiere Defaultwerte");
		setProperty("version", "3.14159");
		setProperty("defaultLocale", "de");

		// placeholder image for image panel
		setProperty("placeholderImage", "015Snapshot3.jpg");

		// relative paths
		setProperty("image.path", "images");

		// encoding stuff
		// setProperty("encoding", "iso-8859-1");
		setProperty("encoding", "UTF-8");

		// ki
		setProperty("kiCommandFifoLength", "5");
		setProperty("kiDataFifoLength", "5");
		setProperty("kiMeanFifiLength", "5");

		// networking
		setProperty("urlCam", "http://192.168.0.23/jpg/image.jpg");
		setProperty("urlSuitcase", "http://192.168.0.20/wlan1.egi");

		// speedLimits
		setProperty("speedMax", "35");
		setProperty("speedMin", "19");
		setProperty("speedStop", "26");
		setProperty("speedIncrement", "1");

		// dir limits
		setProperty("dirMax", "235");
		setProperty("dirMin", "221");
		setProperty("dirCenter", "228");
		setProperty("directionIncrement", "1");
		setProperty("maxSteeringAngle", "40");

		// motor stop limit
		setProperty("motorStopLimit", "200");
		setProperty("motorStopLimitKI", "255");

		// uppdatetime settings
		setProperty("updateTimeCam", "200");
		setProperty("updateTimeSuitcase", "200");
		setProperty("updateTimeCamKI", "500");
		setProperty("updateTimeSuitcaseKI", "1");
		setProperty("updateTimeCamVideo", "200");

		// camera settings
		setProperty("camFPS", "10");
		setProperty("camWidth", "640");
		setProperty("camHeight", "480");
		setProperty("camTmpFileNames", "tmp_image_");
		setProperty("camMovieFileName", "movie_");
		setProperty("camSnapShotFileName", "snapshot_");

		// sleeptime fr threads
		setProperty("sleepTime", "20");
		setProperty("emergencyBreakTime", "200");

		// angles for the sensors
		setProperty("sensor1Angle", "315");
		setProperty("sensor2Angle", "345");
		setProperty("sensor3Angle", "15");
		setProperty("sensor4Angle", "45");

		// SMCVFH configuratioin
		setProperty("smcvfhFifoLenth", "10");

		// Square Range stuff
		setProperty("securityDistFactor", "1.5");
		setProperty("speedConst", "2.5");
		setProperty("securityDist", "10");
		setProperty("maxResultingAngle", "60.037");
		setProperty("wallCasePercentage", "0.1");
		setProperty("closeObjectSituation", "15");
		setProperty("maxResultingAngleWallCase", "30");
		setProperty("maxResultingAngleSideWallCase", "53");
		setProperty("maxWallCaseDist", "80");
		setProperty("maxSideWallCaseDist", "40");
		setProperty("speedConstWallCase", "0.25");
		setProperty("speedConstSideWallCase", "0.5");
		
	}
}
